import {reqGetshopcartInfo,reqDeleteshopcartInfo,reqUpdateIscheckdInfo} from '@/api/index'
const state = {
    shopCart:[]

}
const actions = {
   async getshopcart({commit}){
        let result = await reqGetshopcartInfo()
        if(result.code==200){
            console.log(result)
            commit('GETSHOPCART',result.data)
        }
    },
    async deleteshopcart({commit},skuId){
        let result = await reqDeleteshopcartInfo(skuId)
        if(result.code==200){
            return 'ok'
        }else{
            return Promise.reject(new Error('faile'))
        }
        
    },
    async updatedischecked({commit},{skuId,isChecked}){
        let result = await reqUpdateIscheckdInfo(skuId,isChecked)
        if(result.code==200){
            return 'ok'
        }else{
            return Promise.reject(new Error('faile'))
        }
        
    },
    deleteAllCheckedById({dispatch,getters}){
        console.log(getters)
        let PromiseAll = []
        getters.shopCart.cartInfoList.forEach(item=>{
            let promise = item.isChecked==1?dispatch('deleteshopcart',item.skuId):''
            PromiseAll.push(promise)
          
        })
        return Promise.all(PromiseAll)

    },
    uppdateAllcheaked({state,dispatch},isChecked){
        console.log(state)
        let PromiseAll = []
        state.shopCart[0].cartInfoList.forEach(item=>{
          let promise = dispatch('updatedischecked',{skuId:item.skuId,isChecked:isChecked})
          PromiseAll.push(promise)
        })
        return Promise.all(PromiseAll)
        
    }

}
const mutations = {
    GETSHOPCART(state,value){
        state.shopCart = value

    }


}
const getters = {
    shopCart(state){
        return state.shopCart[0]||{}
    }

}
export default{
    actions,
    mutations,
    state,
    getters

}